package com.dongdongmovie.service.impl;

import com.dongdongmovie.mapper.ResourcesMapper;
import com.dongdongmovie.mapper.RoleResourceMapper;
import com.dongdongmovie.pojo.AdminUser;
import com.dongdongmovie.pojo.Resources;
import com.dongdongmovie.service.Resource_Service;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@DubboService
public class Resource_ServiceImpl implements Resource_Service {

    //注入mapper
    @Autowired
    private ResourcesMapper resourcesMapper;
    @Autowired
    private RoleResourceMapper roleResourcemapper;

    //查询主页tree
    @Override
    public List<Map<String, Object>> show(AdminUser a) {
        List<Resources> list = resourcesMapper.show(a.getUserId());
        return t(list, 0);
    }

    //查询主页ztree
    @Override
    public List<Map<String, Object>> showresource() {
        List<Resources> list = resourcesMapper.selectByExample(null);
        return t1(list, 0);
    }

    //查询主页ztree(角色修改用)
    @Override
    public List<Map<String, Object>> showresources(Integer roleId) {
        List<Integer> list1 = roleResourcemapper.showResId(roleId);
        List<Resources> list = resourcesMapper.selectByExample(null);
        return t2(list1, list, 0);
    }

    //添加数据
    @Override
    public int add(Resources r) {
        return resourcesMapper.insertSelective(r);
    }

    //根据id查询
    @Override
    public Resources showbyId(Integer resId) {
        return resourcesMapper.selectByPrimaryKey(resId);
    }

    //修改数据
    @Override
    public int update(Resources r) {
        return resourcesMapper.updateByPrimaryKeySelective(r);
    }

    //根据id删除
    @Override
    public int delete(Integer resId) {
        return resourcesMapper.deleteByPrimaryKey(resId);
    }

    //查询主页tree使用的递归查询
    public static List<Map<String, Object>> t(List<Resources> list, Integer pid) {
        List<Map<String, Object>> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            if (list.get(i).getPid() == pid) {
                map.put("id", list.get(i).getResId());
                map.put("text", list.get(i).getResName());
                map.put("url", list.get(i).getUrl());
                map.put("nodes", t(list, list.get(i).getResId()));
                if (t(list, list.get(i).getResId()).size() == 0) {
                    map.remove("nodes");
                }
                list1.add(map);
            }
        }
        return list1;
    }

    //查询主页ztree使用的递归查询(角色修改用)
    public static List<Map<String, Object>> t2(List<Integer> lists, List<Resources> list, Integer pid) {
        List<Map<String, Object>> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            if (list.get(i).getPid() == pid) {
                map.put("id", list.get(i).getResId());
                map.put("name", list.get(i).getResName());
                map.put("children", t2(lists, list, list.get(i).getResId()));
                if (lists.contains(list.get(i).getResId())) {
                    map.put("checked", true);
                }
                if (t2(lists, list, list.get(i).getResId()).size() == 0) {
                    map.remove("children");
                }
                list1.add(map);
            }
        }
        return list1;
    }

    //查询主页ztree使用的递归查询
    public static List<Map<String, Object>> t1(List<Resources> list, Integer pid) {
        List<Map<String, Object>> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            if (list.get(i).getPid() == pid) {
                map.put("id", list.get(i).getResId());
                map.put("name", list.get(i).getResName());
                map.put("children", t1(list, list.get(i).getResId()));
                if (t1(list, list.get(i).getResId()).size() == 0) {
                    map.remove("children");
                }
                list1.add(map);
            }
        }
        return list1;
    }
}
